/**
 * 
 */
package org.arpit.masteringextjs.dao.impl;

import java.util.List;

import org.arpit.masteringextjs.dao.DBUtility;
import org.arpit.masteringextjs.dao.GroupsDAO;
import org.arpit.masteringextjs.domain.Groups;
import org.arpit.masteringextjs.domain.Menu;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.ResultTransformer;

/**
 * @author Arpit.Rathore
 *
 */
public class GroupsDAOImpl implements GroupsDAO {
	
	private SessionFactory sessionFactory = DBUtility.getSessionFactory();
	private static GroupsDAO groupsDAO;
	
	private GroupsDAOImpl() {
		
	}
	
	
	@Override
	public Groups createGroups(Groups groups) {
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		Groups createdGroups = (Groups) session.merge(groups);
		session.getTransaction().commit();
		session.close();
		return createdGroups;
	}
	
	
	
	public static GroupsDAO getInstance() {
		if(groupsDAO == null) {
			groupsDAO = new GroupsDAOImpl();
		}
		return groupsDAO;
	}


	@Override
	public List<Groups> findAll() {
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		Criteria crit = session.createCriteria(Groups.class);
		crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		return crit.list();
	}
}
